/**
 * Cascade Select jQuery Plugin v1.0
 * 
 * Copyright (C) 2011 by Tomasz Ulanowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 */

(function( $ ){
	$.fn.cascadeselect = function(options) {
		var settings = {
				multiOptions	: null,
				defaultValue	: null,
				separator		: '<br />'
		};
		var allSelects = [this];
		var allSeparators = [];

		if ( options ) { 
			$.extend(settings, options);
		}
		
		this
			.data('options', settings.multiOptions)
			.data('level', 1)
			.change(onChange);

		addOptions(allSelects[0], settings.multiOptions);
		
		if (settings.defaultValue) {
			$.each(settings.defaultValue, function(key, value){
				$(allSelects[key]).val(value);
				onChange.apply(allSelects[key]);
			});
		}
		else {
			this.change();
		}
		
		return this;
		
		function onChange() {
			var select = $(this);
			var options = select.data('options')[select.val()].options;
			var level = select.data('level');
			
			var removed = allSelects.splice(level, allSelects.length - level);
			$.each(removed, function(key, element){
				$(element).remove();
			});
			removed = allSeparators.splice(level-1, allSeparators.length - level + 1);
			$.each(removed, function(key, element){
				$(element).remove();
			});
			
			if (options) {
				var subselect = createSubSelect(options, level+1);
				var separator = $(settings.separator);
				allSelects.push(subselect[0]);
				allSeparators.push(separator[0]);
				select.after(subselect);
				select.after(separator);
				onChange.apply(subselect);
			}
		}
		
		function createSubSelect(options, level) {
			var select = $('<select/>');
			select
				.attr('id', allSelects[0].attr('id') + '-' + level)
				.attr('class', allSelects[0].attr('class'))
				.attr('name', allSelects[0].attr('name'))
				.data('level', level)
				.data('options', options)
				.change(onChange);
			
			addOptions(select, options);
			
			return select;
		}
		
		function addOptions(select, options) {
			$.each(options, function(key, value){
				var option = $("<option/>").val(key);
				if (typeof value == 'string') {
					option.text(value);
				}
				else {
					option.text(value.label);
				}
				select.append(option);
			});
		}
	};
})( jQuery );